package leetcode.每日一题;

import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/11/26 15:53
 * @description：https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/
 */
public class 二叉搜索树中的插入操作 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public TreeNode insertIntoBST(TreeNode root, int val) {
            if (root == null) {
                root = new TreeNode(val);
                return root;
            }
            TreeNode tmp = root;
            TreeNode node = new TreeNode(val);
            while (true) {
                if (tmp.val > val) {
                    if (tmp.left == null) {
                        tmp.left = node;
                        break;
                    } else {
                        tmp = tmp.left;
                    }
                } else {
                    if (tmp.right == null) {
                        tmp.right = node;
                        break;
                    } else {
                        tmp = tmp.right;
                    }
                }
            }
            return root;
        }

    }

}
